home *** CD-ROM | disk | FTP | other *** search
- // ICONTEST.C
- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <dos.h>
- #include <string.h>
- #include <graphics.h>
-
- #define LMB 1
- #define RMB 2
-
- typedef struct {
- int x,y,width,height,color;
- char str[81];
- } Icon;
-
- int IsMouse(void);
- int MouseClick(void);
- void MouseXY(int *xpos,int *ypos);
- void MouseShow(int YesNo);
- void InitGraphics(void);
- Icon CreateIcon(int xo,int yo,int width,int height,
- int color,char * text);
- void DrawIcon(Icon ic);
- int IconCheck(Icon ikon,int xm,int ym);
- void DrawIconHiLite(Icon ic, int OnOff);
-
- union REGS inregs;
- union REGS outregs;
-
- void main() {
- int xpos, ypos, btn, i;
- Icon icon[4];
-
- icon[0] = CreateIcon(100,100,70,15,RED ," Icon 0");
- icon[1] = CreateIcon(200,100,70,15,GREEN," Icon 1");
- icon[2] = CreateIcon(300,100,70,15,BLUE ," Icon 2");
- icon[3] = CreateIcon(200,130,70,15,LIGHTGRAY," Quit");
-
- InitGraphics();
-
- btn = IsMouse();
- if(!btn) {
- closegraph();
- printf("Mouse Driver Not Installed!\n");
- exit(1);
- }
-
- /* Fill background with gray color */
- setfillstyle(SOLID_FILL,LIGHTGRAY);
- bar3d(0,0,getmaxx(),getmaxy(),0,0);
-
- DrawIcon(icon[0]);
- DrawIcon(icon[1]);
- DrawIcon(icon[2]);
- DrawIcon(icon[3]);
-
- MouseShow(1);
-
- do {
- do {
- MouseXY(&xpos,&ypos);
- btn = MouseClick();
- }while(!btn);
- if(btn == LMB)
- for(i=0;i<4;i++) {
- if(IconCheck(icon[i],xpos,ypos))
- switch(i) {
- case 0: DrawIconHiLite(icon[i], 1);
- printf("\7");
- DrawIconHiLite(icon[i], 0);
- break;
- case 1: DrawIconHiLite(icon[i], 1);
- printf("\7");
- DrawIconHiLite(icon[i], 0);
- break;
- case 2: DrawIconHiLite(icon[i], 1);
- printf("\7");
- DrawIconHiLite(icon[i], 0);
- break;
- case 3: btn = RMB; break;
- }
- }
- }while(btn != RMB);
-
- closegraph();
- }
-
- /* //////////////////////////////////////////////////*/
- void InitGraphics(void)
- {
- int gdriver = DETECT, gmode, errorcode;
- initgraph(&gdriver, &gmode, "");
- if (gdriver != VGA) {
- printf("VGA graphics card required.\n");
- exit(1);
- }
- errorcode = graphresult();
- if (errorcode != grOk) /* an error occurred */
- {
- printf("Graphics error: %s\n", grapherrormsg(errorcode));
- printf("Press any key to halt:");
- getch();
- exit(1); /* terminate with an error code */
- }
- }
-
- Icon CreateIcon(int xo,int yo,int width,int height,int color,char * text)
- {
- Icon temp;
- temp.x = xo;
- temp.y = yo;
- temp.width = width;
- temp.height = height;
- temp.color = color;
- strcpy(temp.str,text);
- return(temp);
- }
-
- void DrawIcon(Icon ic)
- {
- int color;
- color = getcolor();
- MouseShow(0);
- setcolor(0);
- setfillstyle(1,ic.color);
- bar3d(ic.x,ic.y,ic.x+ic.width,ic.y+ic.height,0,0);
- rectangle(ic.x,ic.y,ic.x+ic.width,ic.y+ic.height);
- rectangle(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+ic.height-1);
- outtextxy(ic.x+3,ic.y + 5,ic.str);
- setcolor(15);
- line(ic.x,ic.y,ic.x+ic.width,ic.y);
- line(ic.x,ic.y,ic.x,ic.y+ic.height);
- line(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+1);
- line(ic.x+1,ic.y+1,ic.x+1,ic.y+ic.height-1);
- outtextxy(ic.x+2,ic.y + 4,ic.str);
- MouseShow(1);
- setcolor(color);
- }
-
- int IconCheck(Icon ikon,int xm,int ym)
- {
- if ((xm > ikon.x) && (xm < ikon.x + ikon.width) &&
- (ym > ikon.y) && (ym < ikon.y + ikon.height))
- return(1);
- else return(0);
- }
-
-
- void DrawIconHiLite(Icon ic, int OnOff)
- {
- int color;
- color = getcolor();
- if (OnOff) {
- MouseShow(0);
- setcolor(WHITE);
- rectangle(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+ic.height-1);
- setcolor(BLACK);
- line(ic.x,ic.y,ic.x+ic.width,ic.y);
- line(ic.x,ic.y,ic.x,ic.y+ic.height);
- line(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+1);
- line(ic.x+1,ic.y+1,ic.x+1,ic.y+ic.height-1);
- MouseShow(1);
- }
- else {
- MouseShow(0);
- setcolor(BLACK);
- rectangle(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+ic.height-1);
- setcolor(WHITE);
- line(ic.x,ic.y,ic.x+ic.width,ic.y);
- line(ic.x,ic.y,ic.x,ic.y+ic.height);
- line(ic.x+1,ic.y+1,ic.x+ic.width-1,ic.y+1);
- line(ic.x+1,ic.y+1,ic.x+1,ic.y+ic.height-1);
- MouseShow(1);
- setcolor(color);
- }
- }
-
-
- int IsMouse(void)
- {
- inregs.x.ax = 0;
- int86(0x33,&inregs,&outregs);
- return(outregs.x.ax ? outregs.x.bx : 0);
- }
-
- int MouseClick(void)
- {
- int click = 0;
- inregs.x.ax = 5;
- inregs.x.bx = 1;
- int86(0x33,&inregs,&outregs);
- click = outregs.x.bx << 1;
- inregs.x.bx--;
- int86(0x33,&inregs,&outregs);
- return(click | outregs.x.bx);
- }
-
- void MouseXY(int *xpos,int *ypos)
- {
- inregs.x.ax = 3;
- int86(0x33,&inregs,&outregs);
- *xpos = outregs.x.cx;
- *ypos = outregs.x.dx;
- }
-
-
- void MouseShow(int YesNo)
- {
- if (YesNo) inregs.x.ax = 1;
- else inregs.x.ax = 2;
- int86(0x33,&inregs,&outregs);
- }
-